package ua.kharkov.knure.pereverziev.diploma.web.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;

public class DateTimeUtil {

	private static final Logger log = Logger.getLogger(DateTimeUtil.class);

	public static int[] getDayOfWeek(final String day, final String month,
			final String year) {

		final Calendar calendar = Calendar.getInstance();
		calendar.set(Integer.parseInt(year, 10),
				Integer.parseInt(month, 10) - 1, Integer.parseInt(day, 10));
		log.info(calendar.getTime() + " Day of week "
				+ (calendar.get(Calendar.DAY_OF_WEEK) - 1));
		final int[] days = new int[3];
		final int d = (calendar.get(Calendar.DAY_OF_WEEK) - 1);
		if (d == 1) {
			days[0] = 7;
			days[1] = 1;
			days[2] = 2;
		} else if (d == 0) {
			days[0] = 6;
			days[1] = 7;
			days[2] = 1;
		} else {
			days[0] = d - 1;
			days[1] = d;
			days[2] = d + 1;
		}
		log.info("Day of week " + days);

		return days;
	}

	/**
	 * Creates array of 3 days. First if a day before specified date, second is
	 * actually date, third is a day after specified date.
	 * 
	 * @param day
	 * @param month
	 * @param year
	 * @return Array of dates
	 */
	public static String[] getNeighboringDays(final String day,
			final String month, final String year) {

		final Calendar beforeDay = Calendar.getInstance();
		final Calendar flightDay = Calendar.getInstance();
		final Calendar afterDay = Calendar.getInstance();

		beforeDay.set(Integer.parseInt(year, 10),
				Integer.parseInt(month, 10) - 1, Integer.parseInt(day, 10));

		flightDay.set(Integer.parseInt(year, 10),
				Integer.parseInt(month, 10) - 1, Integer.parseInt(day, 10));

		afterDay.set(Integer.parseInt(year, 10),
				Integer.parseInt(month, 10) - 1, Integer.parseInt(day, 10));

		log.info(flightDay.getTime() + " Day of week "
				+ (flightDay.get(Calendar.DAY_OF_WEEK) - 1));
		final String[] days = new String[3];

		beforeDay.add(Calendar.DATE, -1);
		afterDay.add(Calendar.DATE, 1);

		days[0] = getDateString(beforeDay.getTime());
		days[1] = getDateString(flightDay.getTime());
		days[2] = getDateString(afterDay.getTime());
		return days;
	}

	public static Calendar createDate(final String day, final String month,
			final String year) {
		final Calendar date = Calendar.getInstance();
		date.set(Integer.parseInt(year, 10), Integer.parseInt(month, 10) - 1,
				Integer.parseInt(day, 10));

		log.debug("Date transformed : " + date.getTime());
		return date;
	}

	/**
	 * 
	 * @param dateString
	 *            is a date in format dd.mm.yyyy
	 * @return date
	 */
	public static Date parseDate(final String dateString) {
		final Calendar date = Calendar.getInstance();
		final String[] strings = dateString.replaceAll("\\.", "/").split("/");

		if (strings.length != 3) {
			log.debug("Date substrings length=" + strings.length);
			return null;
		}
		log.debug("Date:" + Integer.parseInt(strings[0], 10) + "-"
				+ (Integer.parseInt(strings[1], 10) - 1) + "-"
				+ Integer.parseInt(strings[2], 10));

		date.set(Integer.parseInt(strings[2], 10),
				Integer.parseInt(strings[1], 10) - 1,
				Integer.parseInt(strings[0], 10));
		return date.getTime();
	}

	public static String getDateString(final Date date) {
		final SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd ");
		return formatter.format(date);
	}

	public static String millisecondsToString(Long duration) {
		String result = "";

		final int SECONDS_IN_MINUTE = 60;
		final int SECONDS_IN_HOUR = 3600;
		final int SECONDS_IN_DAY = 86400;

		if (duration == null || duration == 0l) {
			result = "N/A";
		} else {

			duration /= 1000; // converting to seconds

			if ((duration / SECONDS_IN_DAY) != 0)
				result += (duration / SECONDS_IN_DAY) + "d ";

			if ((duration % SECONDS_IN_DAY) / SECONDS_IN_HOUR != 0)
				result += (duration % SECONDS_IN_DAY) / SECONDS_IN_HOUR + "h ";

			if ((duration % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE != 0)
				result += (duration % SECONDS_IN_HOUR) / SECONDS_IN_MINUTE
						+ "m ";

			if ((duration % SECONDS_IN_MINUTE) != 0)
				result += (duration % SECONDS_IN_MINUTE) + "s ";

			if (result.length() > 2)
				result = result.substring(0, result.length() - 1);
		}
		return result;
	}
}
